function LoadDocumentData() {
    var query = "documents?q=id:" + ParentParam("id");
    var back_id = ParentParam("back");
    if (back_id != "home") query += "+topic:" + back_id;
    RequestLibraryData(query, function(results, terms) {
	ProcessDocumentData(results, terms, back_id);
    });
}

function ProcessDocumentData(results, terms, back_id) {
    var back_url ="";
    var back_text = "";
    if (back_id == "home") {
	back_url = CalculateSiteURL("/library");
	back_text = "library home";
    } else {
	back_url = CalculateSiteURL("/library/topic?topic=" + back_id);
	back_text = terms["topic:" + back_id];
    }
    document.getElementById("topic_link").innerHTML =
	"<a href=\"" + back_url +
	"\" class=\"back\" target=\"_top\">Back to " + back_text +
	"</a>";
    var metadata = results[0];
    var year = ""
    if (metadata.year != null) year = " " + metadata.year + ", ";
    var title_html = "<span class=\"title\">" + metadata.title +
	"</span>, " + year + "<cite>" + metadata.sources.join(', ') +
	"</cite>";
    document.getElementById("metadata_title").innerHTML = title_html;
    // TODO(sdd): insert related documents (when we have them).
    document.getElementById("related_docs").innerHTML = "";
}

function ProcessUserData(obj) {
    if (!obj.data.email) return;
    document.getElementById("sign_out_link").innerHTML =
	'<a href="' + CalculateBackendURL(obj.data.logout_url) +
	'">Sign out</a>';
}
